<?php
App::import('Core', 'HttpSocket');
//App::import('component','Qdmail');
//App::import('Component','Auth');
class UsersController extends AppController {

	var $name = 'Users';
	//var $uses = array('Users','UsersEdit');
	var $uses = array('Users','Comment','Favoritearea','Premiums','Usersedit','Usersearch');
	var $layout = "display";
	var $components = array('Auth','Bench','Cookie','Security','Email');
	//Auth認証不要のアクション
	var $auth_allow = array(
		'regist',
		'userregist',
		'dspuserregist',
		'login',
		'logout',
		'dsprepass',
		'repass'
	);
	
	//==========================================================================
	//コールバック
	//==========================================================================
	function beforeFilter(){
		
		//AppController::beforeFilter()を処理
		parent::beforeFilter();
		
		//フォームのCSRF対策
		$this->Security->requireAuth('edituser','deluser');
		
		$this->Auth->autoRedirect = false;//自動リダイレクトをオフ
		
	}
	
	//----------------------------------------------------------------
	//ユーザ登録画面アクション
	//----------------------------------------------------------------
	function dspuserregist(){
		$this->_pageTitle(SITE_NAME.' : ユーザ登録画面', true);
	}
	
	//----------------------------------------------------------------
	//ユーザ仮登録アクション
	//----------------------------------------------------------------
	function userregist(){
		$this->_pageTitle(SITE_NAME.' : ユーザ仮登録完了登録画面', true);
		
		//入力された値を取得
		$this->Users->set($this->data);
		
		//入力された値のチェック
		if( $this->Users->Validates()){
			
			$savedata = array('Users' => array(
					'shopid' => $this->data['Users']['shopid'],
					'password' => AuthComponent::password($this->data['Users']['passwordhide']),
					'group' => 0,
					'apikey' => $this->data['Users']['apikey'],
					'mail' => $this->data['Users']['mail'],
					'userstatus' => PUBLIC_USER_STATUS_VALID,
					'registstatus' => PUBLIC_REGIST_STATUS_PREREGIST));
			

			//Usersテーブルへ登録
	        //$this->data['Users']['password'] = AuthComponent::password($this->data['Users']['passwordhide']);
	        //$this->data['Users']['userstatus'] =  PUBLIC_USER_STATUS_VALID;   
			//$this->data['Users']['registstatus'] =  PUBLIC_REGIST_STATUS_PREREGIST;   
	        $this->Users->create();
			//if($this->Users->save($this->data)){
			if($this->Users->save($savedata)){
				//ショップID、メールアドレス（暗号化）をパラメータとしたユーザ本登録登録アドレスを作成
		        $url = 'http://dsam.matrix.jp/users/regist/';
		        $url .= $this->data['Users']['shopid']. '/';
		        $url .= $this->Auth->password($this->data['Users']['mail']);
				
		        //$this->Bench->report('mail:'. $url);
		        
				//メールアドレスへユーザ本登録用アドレスを送信
		        
		        $senddata['url']=$url;
		        $senddata['shopid']=$this->data['Users']['shopid'];
		        
		        $this->Email->to = $this->data['Users']['mail'];
		        $this->Email->subject = 'DSAM本登録アドレスの送付';
		        $this->Email->replyTo = PUBLIC_SITE_EMAL;
		        $this->Email->from = PUBLIC_SITE_EMAL;
		        $this->Email->template = 'userregist';
		        $this->Email->layout = 'default';
		        $this->Email->sendAs = 'text';
				$this->set('senddata', $senddata);
				$this->Email->send();
		        
				
				
				//入力値が正常の場合
				$this->set('all_count', -1);
			    
			    //仮登録完了画面へ遷移
				$this->render('tempregist');
        	}
			else{
				//異常終了
				$this->render('regist_error');
			}
		}
		else{
			//入力値のエラーが発生
			$this->render('dspuserregist');
		}
	}
	
	//----------------------------------------------------------------
	//ユーザ本登録アクション
	//----------------------------------------------------------------
	function regist(){
		$this->_pageTitle(SITE_NAME.' : 本登録完了画面', true);
		
		//パラメータのショップIDとメールアドレス（暗号済み）を取得
		$shopid = $this->params['pass'][0];
		$email = $this->params['pass'][1];
		        
		//ユーザデータの取得
		$conditions['shopid'] = $shopid;
		$user = $this->Users->find($conditions);
		if(!empty($user)){
			//データが存在する
			
		//メールアドレスの比較
			if( $this->Auth->password($user['Users']['mail']) == $email){
				//メールアドレス確認成功
			
				//本登録処理
		        $user['Users']['registstatus'] =  PUBLIC_REGIST_STATUS_REGIST;
		        if($this->Users->save($user)){
		        	//本登録処理成功

		        	/**
					//Cookieへ入力されたショップIDを格納
				    $this->Cookie->write('dsam.shopid',$user['Users']['shopid'],true,COOKIE_EXPIRE);
				    if(!empty($user['Users']['apikey'])){
					    $this->Cookie->write('dsam.apikey',$user['Users']['apikey'],true,COOKIE_EXPIRE);
				    }
				    **/
			    
		        	$this->set('user', $user);
					$this->render('regist_done');
		        }
				else{
					//異常終了
			        $this->Bench->report('save_err');
					$this->render('regist_error');
				}
			}
			else{
				//メールアドレス確認失敗なのでエラー画面を表示
		        $this->Bench->report('mail_err');
				$this->render('regist_error');
			}
		}
		else{
			//データが存在しないのでエラー画面を表示
	        $this->Bench->report('data_non');
			$this->render('regist_error');
		}
	}
	
	
	//----------------------------------------------------------------
	//ログイン画面アクション
	//----------------------------------------------------------------
	function login(){
		$this->_pageTitle(SITE_NAME.' : ログイン画面', true);

/**
		if (empty($this->data)) {
            $cookie = $this->Cookie->read('dsam.User');
            if (!is_null($cookie)) {
				//クッキーの情報でログインしてみる。
                if ($this->Auth->login($cookie)) {
                	$this->redirect($this->Auth->redirect());
                }
    	    }
    	}
**/
		
    	if ($this->Auth->user()) {
    		//ログイン成功
    		/**
            if (!empty($this->data['Users']['remember_me'])) {
                $cookie = array();
                $cookie['username'] = $this->data['Users']['shopid'];
                $cookie['password'] = $this->data['Users']['password'];//ハッシュ化されている
                $this->Cookie->write('dsam.User', $cookie, true, '+2 weeks');//3つめの'true'で暗号化
                unset($this->data['User']['remember_me']);
            }
            **/
            $this->redirect($this->Auth->redirect());
        }
        else{
        	//ログインに失敗
            $this->redirect($this->Auth->redirect());
        }
		
	}
	
	//----------------------------------------------------------------
	//ログアウト画面アクション
	//----------------------------------------------------------------
	function logout(){
		$this->_pageTitle(SITE_NAME.' : ログアウト画面', true);
		//$this->Cookie->del('dsam.User');
		$this->Auth->logout();
	}
	
	//----------------------------------------------------------------
	//ランダムな文字列作成アクション
	//----------------------------------------------------------------
	function _getRandomString($nLengthRequired = 8){
	    $sCharList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
	    mt_srand();
	    $sRes = "";
	    for($i = 0; $i < $nLengthRequired; $i++)
	        $sRes .= $sCharList{mt_rand(0, strlen($sCharList) - 1)};
	    return $sRes;
	}
	
	//----------------------------------------------------------------
	//パスワード再発行画面アクション
	//----------------------------------------------------------------
	function dsprepass(){
		$this->_pageTitle(SITE_NAME.' : パスワード再発行画面', true);
	}
	
	//----------------------------------------------------------------
	//パスワード再発行処理アクション
	//----------------------------------------------------------------
	function repass(){
		$this->_pageTitle(SITE_NAME.' : パスワード再発行完了画面', true);
		
		$maill = $this->data['Users']['mail'];
		if(!empty($maill)){
			
			//メールアドレスでユーザを検索
			$conds = array();
			$conds['mail'] = $maill;
			$user = $this->Users->find($conds);
			
			if(!empty($user)){
				
				//パスワードを再発行
				$temppass = $this->_getRandomString();
				
				//仮パスワードを保存する
		        $user['Users']['password'] =  $this->Auth->password($temppass);
		        if($this->Users->save($user)){
		        	//仮パスワード登録成功
		        	
		        	$senddata['mail'] = $maill;
		        	$senddata['pass'] = $temppass;
                	$senddata['shopid'] = $user['Users']['shopid'];
		        	
        	        $this->Email->to = $maill;
			        $this->Email->subject = 'DSAM　お知らせの送付';
			        $this->Email->replyTo = PUBLIC_SITE_EMAL;
			        $this->Email->from = PUBLIC_SITE_EMAL;
			        $this->Email->template = 'repass';
			        $this->Email->layout = 'default';
			        $this->Email->sendAs = 'text';
					$this->set('senddata', $senddata);
					$this->Email->send();
		        	
			
		        	$this->set('mail', $maill);
					$this->render('repass_done');
		        }
				else{
					//異常終了
					$this->render('repass_error');
				}
			}
			else{
				$this->render('repass_error');
			}
		}
	}
	
	//----------------------------------------------------------------
	//ユーザ登録情報画面処理アクション
	//----------------------------------------------------------------
	function showuser(){
		$this->_pageTitle(SITE_NAME.' : ユーザ登録情報画面', true);
		
		//ユーザ登録情報の表示
		$shop_id = $this->Auth->user('shopid');
		
		//ショップIDの値のチェック
		if( !empty($shop_id)){
			//入力値済みの場合
			
			//ユーザデータの取得
			$conds = array();
			$conds['shopid'] = $shop_id;
			$user = $this->Users->find($conds);
			
			if(empty($user)){
				$this->render('showuser_error');
			}
		}
		else{
			$this->securityerror();
			return;
		}
		$user['Users']['password'] = null;
		$this->set('user',$user);
	}
	
	//----------------------------------------------------------------
	//ユーザ登録内容変更画面処理アクション
	//----------------------------------------------------------------
	function dspedituser(){
		$this->_pageTitle(SITE_NAME.' : ユーザ登録内容変更画面', true);
		$this->set('shopid',$this->Auth->user('shopid'));
	}
	
	//----------------------------------------------------------------
	//ユーザ登録内容変更処理アクション
	//----------------------------------------------------------------
	function edituser(){
		$this->_pageTitle(SITE_NAME.' : ユーザ登録内容変更完了画面', true);
		
		//入力内容の取得
		$this->Usersedit->set($this->data);
		$shop_id = $this->Auth->user('shopid');
		
		//入力された値のチェック
		if( $this->Usersedit->Validates()){
		//ショップIDの値のチェック
			if( !empty($shop_id)){
				//入力値済みの場合
				
				//ユーザデータの取得
				$conds = array();
				$conds['shopid'] = $shop_id;
				$user = $this->Users->find($conds);
				
				if(empty($user)){
			        $this->Bench->report('userdata_non_err');
					$this->render('showuser_error');
				}
				else{
					//パスワード確認処理
					if(AuthComponent::password($this->data['Usersedit']['password']) != $user['Users']['password']){
						//パスワード違い
						$this->flash('確認パスワードが登録されているパスワードと違います','/main/main',5);
						return;
					}
					
					//変更内容を保存処理
					
					//入力された内容を設定
			        if(!empty($this->data['Usersedit']['mail'])){
				        $user['Users']['mail'] =  $this->data['Usersedit']['mail'];
        	        }
			        if(!empty($this->data['Usersedit']['newpassword'])){
				        $user['Users']['password'] =  AuthComponent::password($this->data['Usersedit']['newpassword']);
        	        }
			        if(!empty($this->data['Usersedit']['apikey'])){
				        $user['Users']['apikey'] =  $this->data['Usersedit']['apikey'];
        	        }
        	        
        	        //DBへ保存
        	        $this->Users->create();
        	        if($this->Users->save($user,false)){
			        	//本登録処理成功
	
				        if(!empty($this->data['Usersedit']['apikey'])){
						    //キャッシュにAPIコードを格納
							Cache::write('apikey_'. $shop_id,$this->data['Usersedit']['apikey']);
                        }
						$this->setAction('logout');
                        //$this->render('edituser_done');
			        }
					else{
						//異常終了
				        $this->Bench->report('userdata_save_err');
						$this->Bench->report('ID:'. $user['Users']['id']);
				        $this->render('showuser_error');
					}
				}
			}
			else{
				$this->Bench->report('shopid_non_err');
				$this->securityerror();
				return;
			}
		}
		else{
			//入力値のエラーが発生
			$this->Bench->report('input_data_err:'. $this->data['Usersedit']['newpassword']);
			$this->set('shopid',$this->Auth->user('shopid'));
		}
	}
	
	//----------------------------------------------------------------
	//ユーザ登録削除画面処理アクション
	//----------------------------------------------------------------
	function dspdeluser(){
		$this->_pageTitle(SITE_NAME.' : ユーザ登録削除画面', true);
		$this->set('shopid',$this->Auth->user('shopid'));
	}
	
	//----------------------------------------------------------------
	//ユーザ登録削除処理アクション
	//----------------------------------------------------------------
	function deluser(){
		$this->_pageTitle(SITE_NAME.' : ユーザ登録削除完了画面', true);
		
		//入力内容の取得
		$shop_id = $this->Auth->user('shopid');
		
		//ショップIDの値のチェック
		if( !empty($shop_id)){
			
			//ユーザの全データ削除処理
			$conds = array();
			$conds['shopid'] = $shop_id;
            $this->Users->deleteAll($conds);
            $this->Premiums->deleteAll($conds);
            $this->Comment->deleteAll($conds);
            $this->Favoritearea->deleteAll($conds);
            
			//$this->Cookie->del('dsam.shopid');
            
			$this->setAction('logout');
		}
		else{
			$this->securityerror();
			return;
		}
	}
	
	//----------------------------------------------------------------
	//ユーザ管理画面表示アクション
	//----------------------------------------------------------------
	function admin_usermanagement(){
		$user = $this->Auth->user();
		
		//ユーザチェック
		if(empty($user)){
			//未ログインなのでエラーとする
        	$this->Bench->report('usermanagement login err:');
			$this->securityerror();
			return;
		}
		else{
			if (!$user['Users']['group'] == USER_GROUP_MANAGER){
        		$this->Bench->report('usermanagement users group err shopid:'. $user['Users']['shopid']);
				$this->Bench->report('usermanagement users group err group:'. $user['Users']['group']);
				//管理者ユーザでないのでエラーとする
				$this->securityerror();
				return;
			}
		}
		
        $this->Bench->report('usermanagement done shopid:'. $user['Users']['shopid']);
        $this->Bench->report('usermanagement done group:'. $user['Users']['group']);
        
		//ユーザ情報管理画面の表示
		$this->paginate = array(
				'Users' => array(
					'joins' => array(
						array(
							'type' => 'LEFT',
							'alias' => 'Premium',
							'table' => 'dsam_premiums',
							'conditions' => 'Users.id = Premium.uid AND Users.shopid = Premium.shopid'
						)
					),
					'fields' => array(
						'Users.id',
						'Users.shopid',
						'Users.group',
						'Users.apikey',
						'Users.mail',
						'Users.userstatus',
						'Users.registstatus',
						'Premium.paytype',
						'Premium.payfromdate',
						'Premium.paytodate'
					),
					'order' => array('shopid' => 'asc')
				),
			);
			
		$this->set('Users',$this->paginate());
		$this->render('admin_usermanagement');
	}
	
	//----------------------------------------------------------------
	//ユーザ管理画面検索アクション
	//----------------------------------------------------------------
	function admin_usersearch(){
		$user = $this->Auth->user();
		
		//ユーザチェック
		if(empty($user)){
			//未ログインなのでエラーとする
        	$this->Bench->report('usersearch login err:');
			$this->securityerror();
			return;
		}
		else{
			if (!$user['Users']['group'] == USER_GROUP_MANAGER){
        		$this->Bench->report('usersearch users group err shopid:'. $user['Users']['shopid']);
				$this->Bench->report('usersearch users group err group:'. $user['Users']['group']);
				//管理者ユーザでないのでエラーとする
				$this->securityerror();
				return;
			}
		}
		
        $this->Bench->report('usermanagement done shopid:'. $user['Users']['shopid']);
        $this->Bench->report('usermanagement done group:'. $user['Users']['group']);
        
		//検索条件作成
		$conditions = array();
		$this->Usersearch->set($this->data);
		
		//ショップID
		if (!empty($this->data['usersearch']['shopid'])){
			$conditions['Users.shopid'] = $this->data['usersearch']['shopid'];
		}
		//ステータス
		if (!empty($this->data['usersearch']['status'])){
			$conditions['Users.userstatus'] = (int)$this->data['usersearch']['status'];
		}
		//登録情報
		if (!empty($this->data['usersearch']['registstatus'])){
			$conditions['Users.registstatus'] = (int)$this->data['usersearch']['registstatus'];
		}
		//ユーザグループ
		if (!empty($this->data['usersearch']['group'])){
			$conditions['Users.group'] = (int)$this->data['usersearch']['group'];
		}
		//支払い種別
		if (!empty($this->data['usersearch']['paytype'])){
			$conditions['Premium.paytype'] = (int)$this->data['usersearch']['paytype'];
		}
		//支払い開始日 from to
		if (!empty($this->data['usersearch']['payfromdate_from']['year'])){
			$conditions += array('payfromdate >= ' => $this->data['usersearch']['payfromdate_from']['year']. '-'. $this->data['usersearch']['payfromdate_from']['month']. '-'. $this->data['usersearch']['payfromdate_from']['day']. ' 00:00:00') ;
		}
		if (!empty($this->data['usersearch']['payfromdate_to']['year'])){
			$conditions += array('payfromdate <= ' => $this->data['usersearch']['payfromdate_to']['year']. '-'. $this->data['usersearch']['payfromdate_to']['month']. '-'. $this->data['usersearch']['payfromdate_to']['day']. ' 24:00:00') ;
		}
		
		//支払い終了日
		if (!empty($this->data['usersearch']['paytodate_from']['year'])){
			$conditions += array('paytodate >= ' => $this->data['usersearch']['paytodate_from']['year']. '-'. $this->data['usersearch']['paytodate_from']['month']. '-'. $this->data['usersearch']['paytodate_from']['day']. ' 00:00:00') ;
		}
		if (!empty($this->data['usersearch']['paytodate_to']['year'])){
			$conditions += array('paytodate <= ' => $this->data['usersearch']['paytodate_to']['year']. '-'. $this->data['usersearch']['paytodate_to']['month']. '-'. $this->data['usersearch']['paytodate_to']['day']. ' 24:00:00') ;
		}
		
        //$this->Bench->report('usermanagement conditions:'. json_encode($conditions));
        
		$this->paginate = array(
			'Users' => array(
				'conditions' => $conditions,
				'joins' => array(
					array(
						'type' => 'LEFT',
						'alias' => 'Premium',
						'table' => 'dsam_premiums',
						'conditions' => 'Users.id = Premium.uid AND Users.shopid = Premium.shopid'
					)
				),
				'fields' => array(
					'Users.id',
					'Users.shopid',
					'Users.group',
					'Users.apikey',
					'Users.mail',
					'Users.userstatus',
					'Users.registstatus',
					'Premium.paytype',
					'Premium.payfromdate',
					'Premium.paytodate'
				),
				'order' => array('shopid' => 'asc'),
			),
		);
			
		$this->set('Users',$this->paginate());
		$this->render('admin_usermanagement');
		
	}
	
	//----------------------------------------------------------------
	//ユーザ管理画面変更画面表示アクション
	//----------------------------------------------------------------
	function admin_dspuseredit(){
		$user = $this->Auth->user();
		
		//ユーザチェック
		if(empty($user)){
			//未ログインなのでエラーとする
        	$this->Bench->report('edit login err:');
			$this->securityerror();
			return;
		}
		else{
			if (!$user['Users']['group'] == USER_GROUP_MANAGER){
        		$this->Bench->report('edit users group err shopid:'. $user['Users']['shopid']);
				$this->Bench->report('edit users group err group:'. $user['Users']['group']);
				//管理者ユーザでないのでエラーとする
				$this->securityerror();
				return;
			}
		}
		
		//ユーザ検索
		$shopid = $this->params['pass'][0];
		
		//ユーザデータの取得
		$conds = array();
		$conds['shopid'] = $shopid;
		$user = $this->Users->find($conds);
		
		if(empty($user)){
			//存在しないユーザなのでエラー
        	$this->Bench->report('edit user err:'. $shopid. ':');
			$this->securityerror();
			return;
		}
		
		//プレミアム登録データの取得
		$premium = $this->Premiums->find($conds);
		
		$this->set('Users',$user);
		$this->set('Premiums',$premium);
		$this->render('admin_useredit');
	}
	
	//----------------------------------------------------------------
	//ユーザ管理画面変更処理アクション
	//----------------------------------------------------------------
	function admin_useredit(){
		$user = $this->Auth->user();
		
		//ユーザチェック
		if(empty($user)){
			//未ログインなのでエラーとする
        	$this->Bench->report('admin_useredit login err:');
			$this->securityerror();
			return;
		}
		else{
			if (!$user['Users']['group'] == USER_GROUP_MANAGER){
        		$this->Bench->report('admin_useredit  group err shopid:'. $user['Users']['shopid']);
				$this->Bench->report('admin_useredit group err group:'. $user['Users']['group']);
				//管理者ユーザでないのでエラーとする
				$this->securityerror();
				return;
			}
		}
		
		$conditions = array();
		//$this->Usersedit->set($this->data);
		
		//ユーザデータの取得
		$conditions['shopid'] = $user['Users']['shopid'];
		$user = $this->Users->find($conditions);
		
		if(empty($user)){
	        $this->Bench->report('admin_useredit users err:');
			$this->securityerror();
			return;
        }
		
        //ユーザ情報をDBへ保存
        $user['Users']['group'] = $this->data['usersedit']['group'];
        $user['Users']['status'] = $this->data['usersedit']['status'];
        $user['Users']['registstatus'] = $this->data['usersedit']['registstatus'];
        
        $this->Users->create();
        if($this->Users->save($user,false)){
            //プレミアム情報をDBへ保存
        	$premium = $this->Premiums->find($conditions);
        	if(empty($premium)){
	        	$premium['Premiums']['uid'] = $user['Users']['id'];
	        	$premium['Premiums']['shopid'] = $user['Users']['shopid'];
        	   	$premium['Premiums']['status'] = 0;
        	}
        	
        	$premium['Premiums']['paytype'] = $this->data['usersedit']['paytype'];
        	$premium['Premiums']['payfromdate'] = $this->data['usersedit']['payfromdate'];
        	$premium['Premiums']['paytodate'] = $this->data['usersedit']['paytodate'];
        	
            $this->Premiums->create();
	        $this->Premiums->save($premium,false);
        	
        }
        $this->setAction('admin_usermanagement');
	}
	
	//----------------------------------------------------------------
	//ユーザ管理画面削除画面アクション
	//----------------------------------------------------------------
	function admin_dspdelete() {
		$user = $this->Auth->user();
		
		//ユーザチェック
		if(empty($user)){
			//未ログインなのでエラーとする
        	$this->Bench->report('dspdel login err:');
			$this->securityerror();
			return;
		}
		else{
			if (!$user['Users']['group'] == USER_GROUP_MANAGER){
        		$this->Bench->report('dspdel group err shopid:'. $user['Users']['shopid']);
				$this->Bench->report('dspdel group err group:'. $user['Users']['group']);
				//管理者ユーザでないのでエラーとする
				$this->securityerror();
				return;
			}
		}
		
		//ユーザ検索
		$shopid = $this->params['pass'][0];
		
		//ユーザデータの取得
		$conds = array();
		$conds['shopid'] = $shopid;
		$user = $this->Users->find($conds);
		
		if(empty($user)){
			//存在しないユーザなのでエラー
        	$this->Bench->report('dspdel user err:'. $shopid. ':');
			$this->securityerror();
			return;
		}
		
		//プレミアム登録データの取得
		$premium = $this->Premiums->find($conds);
		
		$this->set('Users',$user);
		$this->set('Premiums',$premium);
		$this->render('admin_userdel');
		
	}
		
	//----------------------------------------------------------------
	//ユーザ管理画面削除処理アクション
	//----------------------------------------------------------------
	function admin_userdel(){
		$user = $this->Auth->user();
		
		//ユーザチェック
		if(empty($user)){
			//未ログインなのでエラーとする
        	$this->Bench->report('admin_userdel login err:');
			$this->securityerror();
			return;
		}
		else{
			if (!$user['Users']['group'] == USER_GROUP_MANAGER){
        		$this->Bench->report('aadmin_userdel  group err shopid:'. $user['Users']['shopid']);
				$this->Bench->report('admin_userdel group err group:'. $user['Users']['group']);
				//管理者ユーザでないのでエラーとする
				$this->securityerror();
				return;
			}
		}
		
		$conditions = array();
		
		//ユーザデータの取得
		$conditions['shopid'] = $user['Users']['shopid'];
		$user = $this->Users->find($conditions);
		
		if(empty($user)){
	        $this->Bench->report('admin_userdel users err:');
			$this->securityerror();
			return;
        }
		
        //削除処理
		$conds['shopid'] = $shop_id;
        $this->Users->deleteAll($conditions);
        $this->Premiums->deleteAll($conditions);
        $this->Comment->deleteAll($conditions);
        $this->Favoritearea->deleteAll($conditions);
        
        $this->setAction('admin_usermanagement');
	}
		
}